/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Settings.java
 *
 * Created on Aug 13, 2011, 3:28:35 PM
 */
package azure;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileSystemView;
import org.jdesktop.application.Action;

/**
 *
 * @author Gargravarr
 */
public class Settings extends javax.swing.JFrame
{

    private MainView parentForm;

    /** Creates new form Settings */
    public Settings()
    {
	initComponents();
    }

    @Action
    public void confirmSettings()
    {
	parentForm.getAppSettings().defaultPath = jtBrowserPath.getText();
	parentForm.getAppSettings().autoResume = jcbAutoResume.isSelected();
	closeForm();
    }

    public void showCurrentSettings()
    {
	String currentPath = ((parentForm.getAppSettings().defaultPath.length() > 0)? parentForm.getAppSettings().defaultPath : FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath());
	jtBrowserPath.setText(currentPath);
	jcbAutoResume.setSelected(parentForm.getAppSettings().autoResume);
    }

    /**
     * Hold a reference to the parent form creating this Settings window
     * @param mv 
     */
    public void setParentForm(MainView mv)
    {
	parentForm = mv;
    }

    @Action
    public void showFolderChooser()
    {
	JFileChooser pathFinder = new JFileChooser(jtBrowserPath.getText());
	pathFinder.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	pathFinder.setDialogTitle("Choose Azure Default Directory");
	if (pathFinder.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
	{
	    jtBrowserPath.setText(pathFinder.getSelectedFile().getAbsolutePath());
	}
	checkPath(null);
    }

    @Action
    public void clearLibrary()
    {
	if (JOptionPane.showConfirmDialog(this, "Are you sure you want to clear Azure's library?\nThis cannot be undone!", "Really Clear Library?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
	{
	    SQLibrarian sql = new SQLibrarian();
	    if (sql.clearLibrary() > 0)
	    {
		parentForm.getMangaLibrary().clear();
		parentForm.getAppSettings().lastOpenChapterID = 0;
		parentForm.getAppSettings().lastOpenVolumeID = 0;
		parentForm.getAppSettings().lastOpenMangaID = 0;
		parentForm.getAppSettings().lastOpenPageID = 0;
		JOptionPane.showMessageDialog(this, "Database cleared", "Clear Completed", JOptionPane.INFORMATION_MESSAGE);
	    }
	    else
	    {
		JOptionPane.showMessageDialog(this, "Error: Database could not be cleared. See debug log for more information", "Database Not Cleared", JOptionPane.ERROR_MESSAGE);
	    }
	}
    }

    @Action
    public void closeForm()
    {
	this.dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbCancel = new javax.swing.JButton();
        jbOK = new javax.swing.JButton();
        jtBrowserPath = new javax.swing.JTextField();
        jcboLanguage = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jcbAutoResume = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jbClear = new javax.swing.JButton();
        jbBrowse = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(azure.Main.class).getContext().getResourceMap(Settings.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(azure.Main.class).getContext().getActionMap(Settings.class, this);
        jbCancel.setAction(actionMap.get("closeForm")); // NOI18N
        jbCancel.setText(resourceMap.getString("jbCancel.text")); // NOI18N
        jbCancel.setName("jbCancel"); // NOI18N
        getContentPane().add(jbCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(314, 271, -1, -1));

        jbOK.setAction(actionMap.get("confirmSettings")); // NOI18N
        jbOK.setText(resourceMap.getString("jbOK.text")); // NOI18N
        jbOK.setName("jbOK"); // NOI18N
        getContentPane().add(jbOK, new org.netbeans.lib.awtextra.AbsoluteConstraints(239, 271, -1, -1));

        jtBrowserPath.setText(resourceMap.getString("jtBrowserPath.text")); // NOI18N
        jtBrowserPath.setName("jtBrowserPath"); // NOI18N
        jtBrowserPath.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                checkPath(evt);
            }
        });
        getContentPane().add(jtBrowserPath, new org.netbeans.lib.awtextra.AbsoluteConstraints(169, 49, 231, -1));

        jcboLanguage.setName("jcboLanguage"); // NOI18N
        getContentPane().add(jcboLanguage, new org.netbeans.lib.awtextra.AbsoluteConstraints(169, 16, 231, -1));

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 55, -1, -1));

        jcbAutoResume.setText(resourceMap.getString("jcbAutoResume.text")); // NOI18N
        jcbAutoResume.setToolTipText(resourceMap.getString("jcbAutoResume.toolTipText")); // NOI18N
        jcbAutoResume.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jcbAutoResume.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jcbAutoResume.setName("jcbAutoResume"); // NOI18N
        getContentPane().add(jcbAutoResume, new org.netbeans.lib.awtextra.AbsoluteConstraints(17, 87, -1, -1));

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 133, -1, -1));

        jbClear.setAction(actionMap.get("clearLibrary")); // NOI18N
        jbClear.setText(resourceMap.getString("jbClear.text")); // NOI18N
        jbClear.setName("jbClear"); // NOI18N
        getContentPane().add(jbClear, new org.netbeans.lib.awtextra.AbsoluteConstraints(324, 128, -1, -1));

        jbBrowse.setAction(actionMap.get("showFolderChooser")); // NOI18N
        jbBrowse.setText(resourceMap.getString("jbBrowse.text")); // NOI18N
        jbBrowse.setName("jbBrowse"); // NOI18N
        getContentPane().add(jbBrowse, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 90, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void checkPath(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_checkPath
    if (!(new File(jtBrowserPath.getText()).isDirectory()))
    {
	JOptionPane.showMessageDialog(this, "The Default Directory specified is not valid!", "Invalid Directory", JOptionPane.ERROR_MESSAGE);
	showFolderChooser();
    }
}//GEN-LAST:event_checkPath

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
	/* Set the Nimbus look and feel */
	//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
	 */
	try
	{
	    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
	    {
		if ("Nimbus".equals(info.getName()))
		{
		    javax.swing.UIManager.setLookAndFeel(info.getClassName());
		    break;
		}
	    }
	}
	catch (ClassNotFoundException ex)
	{
	    java.util.logging.Logger.getLogger(Settings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	catch (InstantiationException ex)
	{
	    java.util.logging.Logger.getLogger(Settings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	catch (IllegalAccessException ex)
	{
	    java.util.logging.Logger.getLogger(Settings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	catch (javax.swing.UnsupportedLookAndFeelException ex)
	{
	    java.util.logging.Logger.getLogger(Settings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	//</editor-fold>

	/* Create and display the form */
	java.awt.EventQueue.invokeLater(new Runnable()
	{

	    public void run()
	    {
		new Settings().setVisible(true);
	    }
	});
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jbBrowse;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbClear;
    private javax.swing.JButton jbOK;
    private javax.swing.JCheckBox jcbAutoResume;
    private javax.swing.JComboBox jcboLanguage;
    private javax.swing.JTextField jtBrowserPath;
    // End of variables declaration//GEN-END:variables
}
